require "common/Define"
require "framework/BaseController"
require "FairyGUI"

CurveMain = BaseController:New({mControllerName = "CurveMain"});

function CurveMain.Main()
	-- body
	CurveMain:Awake();
end

function CurveMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- load package
	-- load ui
	local prePackageName = "UI/";
	local packageName = "Curve";
	UIPackage.AddPackage(prePackageName..packageName);

	local go = GameObject.Find("aomian");
	go:GetComponent(typeof(UnityEngine.MeshRenderer)).enabled = true;
	go:GetComponent(typeof(FairyGUI.UIPainter)).enabled = true;

	Camera.main.transform.localPosition = Vector3(250, 8, 0);

	self._mainView = go:GetComponent(typeof(FairyGUI.UIPainter)).ui;
	
	self._list = self._mainView:GetChild("list").asList;
	self._list:SetVirtualAndLoop();
	self._list.itemRenderer = self.RenderListItem;
	self._list.numItems = 5;
	self._list.scrollPane.onScroll:Add(self.DoSpecialEffect, self);


	self:DoSpecialEffect();
end

function CurveMain:DoSpecialEffect()
	-- body
	-- change the scale according to the distance to middle
	local midX = self._list.scrollPane.posX + self._list.viewWidth / 2;
	local cnt = self._list.numChildren;
	for i=0, cnt-1, 1 do
		local obj = self._list:GetChildAt(i);
		local dist = Mathf.Abs(midX - obj.x - obj.width / 2);
		if (dist > obj.width) then
			obj:SetScale(1, 1);
		else
			local ss = 1 + (1 - dist / obj.width) * 0.24;
			obj:SetScale(ss, ss);
		end
	end
end

function CurveMain.RenderListItem(index, obj)
	local item = obj;
	item:SetPivot(0.5, 0.5);
	item.icon = UIPackage.GetItemURL("Curve", "n" .. (index + 1));
end

function CurveMain:OnKeyDown(context)
	if (context.inputEvent.keyCode == KeyCode.Escape) then
		Application.Quit();
	end
end


